<?php
class RecServAdminFormatter implements Formatter {
    private $data;

    function __construct($result) {
        $this->data = $result;
    }
    
    function generateResult() {
        $result = array();
        for ($i = 0; $i < count($this->data); $i++) {
            $result[] = array(
                id => $this->data[$i]['id'],
                cell => array(
                    $this->data[$i]['companyname'] ,
                    $this->data[$i]['company_id'] ,
                ),
            );
        }
        return $result;
    }

    private function getServiceType($value) {
        switch ($value) {
            case "1" : { return "Accounting"; }
            case "2" : { return "Advertising and PR agencies"; }
            case "3" : { return "Air tickets, Airlines"; }
            case "4" : { return "Art galleries"; }
            case "5" : { return "Attorneys"; }
            case "6" : { return "Auditing"; }
            case "7" : { return "Banks"; }
            case "8" : { return "Beauty salons, Skincare, Hair, Cosmetics"; }
            case "9" : { return "Books"; }
            case "10" : { return "Car importers, Fleet management"; }
            case "11" : { return "Car rentals"; }
            case "12" : { return "Casinos"; }
            case "13" : { return "Children's entertainment"; }
            case "14" : { return "Cleaning and security"; }
            case "15" : { return "Computers, software"; }
            case "16" : { return "Conference centers"; }
            case "17" : { return "Courier and messenger services"; }
            case "18" : { return "Culture"; }
            case "19" : { return "Customs agents"; }
            case "20" : { return "Debt management"; }
            case "21" : { return "Event organizors and catering"; }
            case "22" : { return "Fitness clubs"; }
            case "23" : { return "Florists, gardening"; }
            case "24" : { return "Furniture, home decoration"; }
            case "25" : { return "Golf centers"; }
            case "26" : { return "Healthcare and pharmacies"; }
            case "27" : { return "Hostess service"; }
            case "28" : { return "Hotels"; }
            case "29" : { return "Insurance companies"; }
            case "30" : { return "Logistic services"; }
            case "31" : { return "Market research"; }
            case "32" : { return "Office supply"; }
            case "33" : { return "Printing"; }
            case "34" : { return "Real estate"; }
            case "35" : { return "Recruiting and HR services"; }
            case "36" : { return "Relocation"; }
            case "37" : { return "Restaurants"; }
            case "38" : { return "Schools and universities"; }
            case "39" : { return "Shipping"; }
            case "40" : { return "Shopping"; }
            case "41" : { return "Tax consultants"; }
            case "42" : { return "Taxi"; }
            case "43" : { return "Telecommunication"; }
            case "44" : { return "Translators"; }
            case "45" : { return "Travel agencies"; }
        }
    }
}

class Recommended_ServicesModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("recommended_services", "id");
        $this->addField("id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("service_type", Model::TYPE_INT);
        $this->addField("service_description", Model::TYPE_VARCHAR);
        $this->addField("update", Model::TYPE_TEXT, Model::MOD_UPDATE);
        $this->addField("company_id", Model::TYPE_INT, Model::MOD_FOREIGN, array(
            remoteTableModel => "companies",
        ));
        $this->addField("companyname", Model::TYPE_VARCHAR, Model::MOD_REMOTE, array(
            remoteTableModel => "companies",
        ));
        $this->addField("hphungarian", Model::TYPE_VARCHAR, Model::MOD_REMOTE, array(
            remoteTableModel => "companies",
        ));
        $this->addField("logo_id", Model::TYPE_INT, Model::MOD_REMOTE, array(
            remoteTableModel => "companies",
        ));
        $this->addField("accepted", Model::TYPE_INT, Model::MOD_ACCEPT);
        $this->addField("accepted_at", Model::TYPE_DATE_ADDITION, Model::MOD_ACCEPTDATE);
    }

    public static function getServiceType($value) {
        switch ($value) {
            case "1" : { return "Accounting"; }
            case "2" : { return "Advertising and PR agencies"; }
            case "3" : { return "Air tickets, Airlines"; }
            case "4" : { return "Art galleries"; }
            case "5" : { return "Attorneys"; }
            case "6" : { return "Auditing"; }
            case "7" : { return "Banks"; }
            case "8" : { return "Beauty salons, Skincare, Hair, Cosmetics"; }
            case "9" : { return "Books"; }
            case "10" : { return "Car importers, Fleet management"; }
            case "11" : { return "Car rentals"; }
            case "12" : { return "Casinos"; }
            case "13" : { return "Children's entertainment"; }
            case "14" : { return "Cleaning and security"; }
            case "15" : { return "Computers, software"; }
            case "16" : { return "Conference centers"; }
            case "17" : { return "Courier and messenger services"; }
            case "18" : { return "Culture"; }
            case "19" : { return "Customs agents"; }
            case "20" : { return "Debt management"; }
            case "21" : { return "Event organizors and catering"; }
            case "22" : { return "Fitness clubs"; }
            case "23" : { return "Florists, gardening"; }
            case "24" : { return "Furniture, home decoration"; }
            case "25" : { return "Golf centers"; }
            case "26" : { return "Healthcare and pharmacies"; }
            case "27" : { return "Hostess service"; }
            case "28" : { return "Hotels"; }
            case "29" : { return "Insurance companies"; }
            case "30" : { return "Logistic services"; }
            case "31" : { return "Market research"; }
            case "32" : { return "Office supply"; }
            case "33" : { return "Printing"; }
            case "34" : { return "Real estate"; }
            case "35" : { return "Recruiting and HR services"; }
            case "36" : { return "Relocation"; }
            case "37" : { return "Restaurants"; }
            case "38" : { return "Schools and universities"; }
            case "39" : { return "Shipping"; }
            case "40" : { return "Shopping"; }
            case "41" : { return "Tax consultants"; }
            case "42" : { return "Taxi"; }
            case "43" : { return "Telecommunication"; }
            case "44" : { return "Translators"; }
            case "45" : { return "Travel agencies"; }
        }
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function isSponsored($record, $type) {
        $result = false;
        if ($type == "logo") {
            if (dateIsPast($record['spn_service_logo'])) { $result = false; } else { $result = true; }
        } else if ($type == "minibanner") {
            if (dateIsPast($record['spn_service_minibanner'])) { $result = false; } else { $result = true; }
        } else if ($type == "topic_sponsor") {
            if (dateIsPast($record['spn_service_selection'])) { $result = false; } else { $result = true; }
        }
        return $result;
    }

    public function getServiceCount($servicetype) {
    }

    public function getMinibannerservicesByType($serviceType) {
        $query = "SELECT * FROM recommended_services ".
                 "JOIN companies ON companies.company_id=recommended_services.company_id ".
                 "WHERE spn_service_minibanner>=now() AND service_type=$serviceType"
        ;
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        for ($i = 0; $i < count($result); $i++) {
            if ($result[$i]['file_id'] == "") {
                $result[$i]['logo'] = "index.php?mode=service&page=fileloader&service=display&_file_id=" . $result[$i]['minibanner_id'];
            } else {
                $result[$i]['logo'] = "index.php?mode=service&page=fileloader&service=display&_file_id=" . $result[$i]['file_id'];
            }
        }
        return $result;
    }

    public function getTopicSponsorServicesByType($serviceType) {
        $query = "SELECT * FROM recommended_services ".
                 "JOIN companies ON companies.company_id=recommended_services.company_id ".
                 "WHERE spn_service_selection>=now() AND service_type=$serviceType"
        ;
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        for ($i = 0; $i < count($result); $i++) {
            if ($result[$i]['file_id'] == "") {
                $result[$i]['logo'] = "index.php?mode=service&page=fileloader&service=display&_file_id=" . $result[$i]['topic_sponsor_id'];
            } else {
                $result[$i]['logo'] = "index.php?mode=service&page=fileloader&service=display&_file_id=" . $result[$i]['file_id'];
            }
        }
        return $result;
    }

    public function getByServiceType($serviceType) {
        $query = "SELECT * FROM recommended_services ".
                 "JOIN companies ON companies.company_id=recommended_services.company_id ".
                 "WHERE service_type = $serviceType"
        ;
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        for ($i = 0; $i < count($result); $i++) {
            if ($this->isSponsored($result[$i], "logo")) {
                if ($result[$i]['file_id'] == "") {
                    $result[$i]['logo'] = "index.php?mode=service&page=fileloader&service=display&_file_id=" . $result[$i]['sponsored_logo_id'];
                } else {
                    $result[$i]['logo'] = "index.php?mode=service&page=fileloader&service=display&_file_id=" . $result[$i]['file_id'];
                }
            }
        }
        return $result;
    }

    public function getUpdateRequests() {
        $query = "SELECT recommended_services.id, companyname FROM recommended_services ".
                 "JOIN companies ON companies.company_id=recommended_services.company_id ".
                 "WHERE (companies.update_request is not null AND companies.update_request <> '') OR (recommended_services.update IS NOT NULL AND recommended_services.update <> '')"
        ;
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[$i]['id'] = $result[$i]['id'];
            $data[$i]['cell'] = array(
                $result[$i]['companyname']
            );
        }
        return $data;
    }

    public function getPreview($id) {
        $query = "SELECT * FROM recommended_services ".
                 "JOIN companies ON companies.company_id=recommended_services.company_id ".
                 "WHERE id=$id"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        $result = $this->getPreviewData($data[0]);
        if (count($result) > 0) {
            $result['company_id'] = $data[0]['company_id'];
        } else {
            $result = $data[0];
        }
        $result['service_type'] = self::getServiceType($result['service_type']);
        return $result;
    }

    public function getSince($date, $condition = "") {
        $query = "SELECT * FROM recommended_services ".
                 "JOIN companies ON companies.company_id=recommended_services.company_id ".
                 "WHERE companies.created_at >= '$date'"
        ;
        if ($condition != "") { $query .= " AND $condition"; }
        $query .= " LIMIT 0, 10";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        return $data;
    }
/*
    public function getUpdateRequests() {
        $query = "SELECT DISTINCT id, company_name FROM recommended_services WHERE accepted IS NULL OR accepted <> 1";
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[$i]['id'] = $result[$i]['id'];
            $data[$i]['cell'] = array(
                $result[$i]['company_name']
            );
        }
        return $data;
    }
*/
    public function getImage($type, $companyId) {
        $query = "";
        if ($type == "rotator") {
            $query = "SELECT rotator_id AS id FROM companies WHERE company_id=$companyId";
        } else if ($type == "minibanner") {
            $query = "SELECT minibanner_id AS id FROM recommended_services WHERE company_id=$companyId";
        }
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        return $data[0]['id'];
    }

    public function getUpdateRequestCount() {
        $query = "SELECT count(*) AS c  FROM (".
                    "SELECT DISTINCT id, company_name FROM recommended_services ".
                     "WHERE accepted IS NULL OR accepted <> 1".
                 ")x"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data[0]['c'];
    }

    public function getCompanyId($recserv_id) {
        $query = "SELECT company_id FROM recommended_services WHERE id=$recserv_id";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data[0]['company_id'];
    }


    public function isAuthorized($operation, $subject = "") {
        return true;
//        if (Auth::isAdmin()) { return true; }
/*
        switch ($operation) {
            case "update" :
            case "delete" :
            case "addnew" : {
                if (Auth::canChangeDrivers()) { return true; }
            }
            case "list" : {
                if (Auth::canListDrivers()) { return true; }
            }
        }
        return false;
*/
    }

    public function modifyDatabaseInput($data) {
        $result = array();
        return $result;
    }
}

?>
